package com.fick.hibernate.dao;

// default package

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.fick.hibernate.model.Candidate;

/**
 * Data access object (DAO) for domain model class Candidate.
 * 
 * @see .Candidate
 * @author MyEclipse Persistence Tools
 */

public class CandidateDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(CandidateDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String SEX = "sex";
	public static final String ISMARRY = "ismarry";
	public static final String SCHOLL = "scholl";
	public static final String ACADEMIC = "academic";
	public static final String SPECIALITY = "speciality";
	public static final String OFFSCHOLLTIME = "offscholltime";
	public static final String TEL = "tel";
	public static final String EMAIL = "email";
	public static final String ADDRESS = "address";
	public static final String CAPACITY = "capacity";
	public static final String RESUME = "resume";

	public void attachClean(Candidate instance) {
		log.debug("attaching clean Candidate instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachDirty(Candidate instance) {
		log.debug("attaching dirty Candidate instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Candidate instances");
		try {
			String queryString = "from com.fick.hibernate.model.Candidate";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByAcademic(Object academic) {
		return findByProperty(ACADEMIC, academic);
	}

	public List findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	public List findByCapacity(Object capacity) {
		return findByProperty(CAPACITY, capacity);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByExample(Candidate instance) {
		log.debug("finding Candidate instance by example");
		try {
			List results = getSession().createCriteria("com.fick.hibernate.model.Candidate").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public Candidate findById(java.lang.Integer id) {
		log.debug("getting Candidate instance with id: " + id);
		try {
			Candidate instance = (Candidate) getSession().get("com.fick.hibernate.model.Candidate", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByIsmarry(Object ismarry) {
		return findByProperty(ISMARRY, ismarry);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByOffscholltime(Object offscholltime) {
		return findByProperty(OFFSCHOLLTIME, offscholltime);
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Candidate instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from com.fick.hibernate.model.Candidate as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByResume(Object resume) {
		return findByProperty(RESUME, resume);
	}

	public List findByScholl(Object scholl) {
		return findByProperty(SCHOLL, scholl);
	}

	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List findBySpeciality(Object speciality) {
		return findByProperty(SPECIALITY, speciality);
	}

	public List findByTel(Object tel) {
		return findByProperty(TEL, tel);
	}

	public List findPageByHql(String hql, Integer currPage, Integer pageSize) {
		log.debug("find all Candidate by page  currPage is error");
		try {
			String queryString = "from Candidate " + hql;
			log.warn(queryString);
			Query query = getSession().createQuery(queryString);
			query.setFirstResult((currPage - 1) * pageSize);
			query.setMaxResults(pageSize);
			List li = query.list();
			return li;
		} catch (RuntimeException re) {
			log.error("find all Candidate by page failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public Candidate merge(Candidate detachedInstance) {
		log.debug("merging Candidate instance");
		try {
			Candidate result = (Candidate) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
}